const cachePrefix = __APP_ENV__.VITE_CACHE_PREFIX
export const trans = (data) => {
	const transData = []
	const go = (data) => {
		for (const item of data) {
			const {status, parentId, name, id, children, disableCheckbox} = item
			const newItem = {
				parentId,
				name,
				id,
				status,
				disableCheckbox
			}
			transData.push(newItem)
			if (children) {
				go(children)
			}
		}
	}
	go(data)
	return transData
}
export const listToTree = (list) => {
	let map = {},
		node,
		roots = [],
		i
	for (i = 0; i < list.length; i += 1) {
		map[list[i].id] = i
		list[i].children = []
	}
	for (i = 0; i < list.length; i += 1) {
		node = list[i]
		if (node.parentId !== '0' && list[map[node.parentId]]) {
			list[map[node.parentId]].children.push(node)
		} else {
			roots.push(node)
		}
	}
	return roots
}
//把带有斜杠的字符串 , 替换斜杠 , 把斜杠跟随的字母转成大写
// 例如 sys/org/index 会转成 sysOrgIndex
export const toCamelCaseWithDelimiter = ({str, delimiter = '/'}) => {
	return str.split(delimiter).map((part, index) => {
		if (index === 0) {
			// 第一个部分保持原样（不转成大写）
			return part;
		}
		// 转成大写并连接
		return part.charAt(0).toUpperCase() + part.slice(1);
	}).join('');
}

export const removeCache = ({key, sysKey}) => {
	let systemCache = localStorage.getItem(cachePrefix + sysKey)
	if (!systemCache) return
	systemCache = JSON.parse(systemCache)
	delete systemCache[key]
	localStorage.setItem(cachePrefix + sysKey, JSON.stringify(systemCache))
}
export const setCache = ({key, sysKey, value}) => {
	let systemCache = localStorage.getItem(cachePrefix + sysKey)
	if (!systemCache) {
		systemCache = '{}'
	}
	systemCache = JSON.parse(systemCache)
	systemCache[key] = value ? value : {}
	localStorage.setItem(cachePrefix + sysKey, JSON.stringify(systemCache))
}
export const getCache = ({key, sysKey}) => {
	let systemCache = localStorage.getItem(cachePrefix + sysKey)
	if (!systemCache) return false
	try {
		return JSON.parse(systemCache)[key]
	} catch (e) {
		return false
	}
}
